package com.information.manage.basic.dataOperator;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.information.manage.basic.model.entity.StaffDO;
import com.information.manage.basic.model.entity.StaffDetailDO;
import com.information.manage.basic.common.recycling.RecoveryDataOperateManager;
import com.information.manage.basic.common.recycling.RecoveryDataOperator;
import com.information.manage.basic.common.recycling.RecoveryException;
import com.information.manage.basic.mapper.StaffDetailMapper;
import com.information.manage.basic.mapper.StaffMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StaffDetailDataOperator implements RecoveryDataOperator {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private StaffDetailMapper staffDetailMapper;

    @Resource
    private RecoveryDataOperateManager recoveryDataOperateManager;

    @PostConstruct
    private void init(){
        recoveryDataOperateManager.addDataOperate(this);
    }

    @Override
    public void deleteData(Class entityClass, List<Integer> ids) {
        if (entityClass.equals(StaffDO.class)){
            List<Integer> detailIds = staffMapper.selectObjs(new LambdaQueryWrapper<StaffDO>()
                    .select(StaffDO::getStaffDetailId)
                    .in(StaffDO::getId, ids)).stream().map(it -> (Integer) it).collect(Collectors.toList());
            int count = staffDetailMapper.deleteBatchIds(detailIds);
            if (count != ids.size()) throw new RecoveryException("员工详情删除失败，员工详情ids = " + ids);
        }
    }

    @Override
    public List<Class> dependencies() {
        return Arrays.asList(StaffDO.class);
    }

    @Override
    public Class getEntityClass() {
        return StaffDetailDO.class;
    }
}
